home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 1 of 3.iso
/
chapte25
/
ex6.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-23
|
3KB
|
70 lines
#include <genstub.c>
// Windows message procedure.
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch ( uMsg ) {
case WM_CREATE:
DefWindowProc( hWnd, uMsg, wParam, lParam );
// if child set child name.
if ( strstr( GetCommandLine( ), "CHILD" ) )
SetWindowText( hWnd, "CHILD" );
else // position parent to left of child
MoveWindow( hWnd, 0, 0, 200, 200, TRUE );
break;
case WM_COMMAND: // process menu items
switch ( LOWORD( wParam ) ) {
case IDM_TEST: {
HANDLE hAlias;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof( STARTUPINFO ) );
ZeroMemory( &pi, sizeof( PROCESS_INFORMATION ) );
si.cb=sizeof( STARTUPINFO );
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
CreateProcess( "GENERIC.EXE", "CHILD", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
if ( pi.hProcess ) { // If we can make the process.
HWND hWndExternal;
HANDLE hMutex = CreateMutex( NULL, FALSE, "TEST_MUTEX" );
DuplicateHandle( GetCurrentProcess( ), hMutex,
pi.hProcess, &hAlias, SYNCHRONIZE, FALSE, 0 );
// Let initialization of new process occur.
WaitForInputIdle( GetCurrentProcess( ), INFINITE );
// Get external window address.
while ( ( hWndExternal = FindWindow( lpszAppName, "CHILD" ) )==0 );
// move it over to left
MoveWindow( hWndExternal, 200, 0, 200, 200, TRUE );
ShowWindow( hWndExternal, SW_SHOWNORMAL );
PostMessage( hWndExternal, WM_USER, ( WPARAM )hAlias, 0 );
CloseHandle( hMutex );
Sleep( 5000 );
TerminateProcess( pi.hProcess, 0 );
}
}
break;
case IDM_EXIT:
DestroyWindow( hWnd );
break;
}
break;
case WM_USER: {
HDC hDC = GetDC( hWnd );
TextOut( hDC, 0, 0, "Waiting for mutex", 17);
WaitForSingleObject( ( HANDLE )wParam, INFINITE ); // get mutex.
TextOut( hDC, 0, 20, "Got mutex", 9);
Sleep( 3000 );
TextOut( hDC, 0, 40, "Released mutex", 14);
ReleaseMutex( ( HANDLE )wParam );
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
return ( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
}
return ( NULL );
}